/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// abilityType: 0-Ability; 1-Internal Ability
const ABILITY_TYPE_EXTERNAL = 0;
const ABILITY_TYPE_INTERNAL = 1;

// syncOption(Optional, pair sync): 0-Sync; 1-Async
const ACTION_SYNC = 0;
const ACTION_ASYNC = 1;

// Java side register info
const BUNDLE_NAME = 'com.huawei.health.ecology.fa';
const ABILITY_NAME = 'com.huawei.health.ecology.fa.central.client.CentralJsClient';

// Operation Code
const CHECK_PERMISSION = 2001;
const ON_PERMISSION_REQUEST = 2002;
const REQUEST_PERMISSION = 2003;
const START_ABILITY = 2004;
const OPEN_URL = 2005;
const GET_UNIQUE_ID = 2006;
const START_AUDIO_PLAY = 2007;
const STOP_AUDIO_PLAY = 2008;
const PERFORM_PROXY_REGISTER = 2009;
const TAG = 'JS/Operator/Common: '
var ResponseCode;

(function (ResponseCode) {
  ResponseCode[ResponseCode['OPERATION_SUCCESS'] = 10000] = 'OPERATION_SUCCESS';
  ResponseCode[ResponseCode['OPERATION_FAILED'] = 10001] = 'OPERATION_FAILED';
})(ResponseCode || (ResponseCode = {}));

var getRequestAction = function (requestCode) {
  return {
    bundleName: BUNDLE_NAME,
    abilityName: ABILITY_NAME,
    abilityType: ABILITY_TYPE_INTERNAL,
    syncOption: ACTION_SYNC,
    messageCode: requestCode,
  };
};

export default {
  data: {},
  checkPermission: async function(permissions) {
    let actionData = {};
    actionData.permissions = permissions;
    let action = getRequestAction(CHECK_PERMISSION);
    action.data = actionData;
    let resultStr = await FeatureAbility.callAbility(action);
    console.debug(TAG + 'Check Permission result is:' + resultStr);
    return JSON.parse(resultStr);
  },
  onPermissionRequest: async function(callback) {
    let action = getRequestAction(ON_PERMISSION_REQUEST);
    let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
      let callbackJson = JSON.parse(callbackData);
      callback(callbackJson);
    });
    console.debug(TAG + 'Request Permission result is:' + resultStr);
    return JSON.parse(resultStr);
  },
  requestPermission: async function(permissions) {
    let actionData = {};
    actionData.permissions = permissions;
    let action = getRequestAction(REQUEST_PERMISSION);
    action.data = actionData;
    let resultStr = await FeatureAbility.callAbility(action);
    console.debug(TAG + 'Request Permission operation is:' + resultStr);
    return JSON.parse(resultStr);
  },
  startAbility: async function(bundleName, abilityName, params, options) {
    let actionData = {};
    actionData.bundleName = bundleName;
    actionData.abilityName = abilityName;
    actionData.intentParams = JSON.stringify(params);
    actionData.intentOptions = options;
    let action = getRequestAction(START_ABILITY);
    action.data = actionData;
    let resultStr = await FeatureAbility.callAbility(action);
    console.debug(TAG + 'Start Ability result is:' + resultStr);
    let resultObj = JSON.parse(resultStr);
    return resultObj.code == ResponseCode.OPERATION_SUCCESS;
  },
  openUrl: async function(url) {
    if (!url) {
      console.error(TAG + 'actionData.url is empty or undefined');
      return;
    }
    let action = getRequestAction(OPEN_URL);
    action.data = url;
    let resultStr = await FeatureAbility.callAbility((action));
    console.debug(TAG + 'OpenUrl result is:' + resultStr);
    let resultObj = JSON.parse(resultStr);
    return resultObj.code == ResponseCode.OPERATION_SUCCESS;
  },
  getUniqueId: async function() {
    console.debug(TAG + 'get uniqueId');
    let action = getRequestAction(GET_UNIQUE_ID);
    let resultStr = await FeatureAbility.callAbility(action);
    console.debug(TAG + 'get uniqueId result is:' + resultStr);
    return JSON.parse(resultStr);
  },
  startAudioPlay: async function(audioList) {
    let action = getRequestAction(START_AUDIO_PLAY);
    let actionData = {};
    actionData.audioFileList = audioList;
    action.data = actionData;
    let resultStr = await FeatureAbility.callAbility(action);
    console.debug(TAG + 'startAudioPlay result is:' + resultStr);
    let resultObj = JSON.parse(resultStr);
    return resultObj.code == ResponseCode.OPERATION_SUCCESS;
  },
  stopAudioPlay: async function() {
    let action = getRequestAction(STOP_AUDIO_PLAY);
    let resultStr = await FeatureAbility.callAbility(action);
    console.debug(TAG + 'stopAudioPlay result is:' + resultStr);
    let resultObj = JSON.parse(resultStr);
    return resultObj.code == ResponseCode.OPERATION_SUCCESS;
  },
  performProxyRegister: async function(uuid, productId) {
    let action = getRequestAction(PERFORM_PROXY_REGISTER);
    action.data = {
      registerUuid:uuid,
      productId: productId
    };
    let resultStr = await FeatureAbility.callAbility(action);
    console.debug(TAG + 'performProxyRegister result is:' + resultStr);
    let resultObj = JSON.parse(resultStr);
    return resultObj.code == ResponseCode.OPERATION_SUCCESS;
  }
}